'''
Created on Apr 8, 2012

@author: e911miri
'''
from helper import sanitize, runQuery

def result(reg_num=0, pin='0000'): 
    candidate = sanitize(reg_num, pin)
    if candidate:
        query = '''
            select * from candidates join scores where
            reg_num= %s and pin= %s
            and idscores = idcandidates limit 1
        '''
        result = runQuery(query, (candidate['reg_num'], candidate['pin']), True)
        if result:
            score = result[7:11]
            return {'reg_num': reg_num, 'name': result[3], 'score': score, 'combo': result[4], 'total': sum(score)}           
                
        return {'reg_num': reg_num, 'error': 'unreleased'}
    error = 'pin/registration number error'
    return {'reg_num': reg_num, 'error': error}

def getresult(id=None):
    result = runQuery("SELECT reg_num, pin FROM candidates LIMIT 0,1", single=True)
    if result:       
        return{'regnum': result[0], 'pin': result[1]}
    else: return None
